﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using GeneralLibs;
using System.Net.Mail;
using System.Data.SqlClient;

namespace GeneralLibs.Lists
{
    public class DaiDien : DoiTuong
    {
        #region int MaDaiDien property
        public int MaDaiDien
        {
            get
            {
                if (_DataRow["MaDaiDien"] is int) return (int)_DataRow["MaDaiDien"];
                return -1;
            }
            set
            {
                _DataRow["MaDaiDien"] = value;
            }
        }
        #endregion

        #region string DienGiai property
        public string DienGiai
        {
            get
            {
                if (_DataRow["DienGiai"] is string) return (string)_DataRow["DienGiai"];
                return "";
            }
            set
            {
                _DataRow["DienGiai"] = value;
            }
        }
        #endregion

        public DaiDien(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucDaiDien: DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucDaiDien _Instance;
        public static DanhMucDaiDien Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucDaiDien();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    _Instance.DataView.Sort = "MaDaiDien";
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "DaiDien";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new DaiDien((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public DaiDien this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new DaiDien(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public DaiDien NewDoiTuong()
        {
            try
            {
                return new DaiDien(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DaiDien GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new DaiDien(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DaiDien GetByID(Guid objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        public DaiDien GetByMa(int ma)
        {
            try
            {
                return GetByProperty("MaDaiDien", ma);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        public static DanhMucDaiDien Filter(string filterExpression)
        {
            try
            {
                DanhMucDaiDien obj = new DanhMucDaiDien();
                obj.DataView = new DataView(DanhMucDaiDien.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucDaiDien GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}